<?php
/**
 * @package aloevera-biorun
 * @since aloevera-biorun 1.0
 */
get_header();
?>

<section id="primary" class="site-content-category">
    <div id="content" role="main">

<?php if (have_posts()) : ?>
            <header class="archive-header">
                <h1 class="archive-title">
                    <?php printf(__('%s', 'aloevera_biorun'), '<span>' . single_cat_title('', false) . '</span>'); ?>
                 </h1>

                                <?php if (category_description()) : ?>
                                    <div class="archive-meta"><?php echo category_description(); ?></div>
                    <?php endif; ?>
            </header><!-- .archive-header -->




            <?php
            if (is_category()) {
                global $wp_query;
                $category_id = $wp_query->query_vars['cat'];
                
                    if (get_category_children($category_id) != "") {
                        $i = 0;
                        $all_first_cat_title = '';
                        

                        $categories =  get_categories(array('child_of'=>$category_id, 'orderby'=>'ID'));  

                            foreach  ($categories as $category) {
                                
                                if ($i == 0){
                                        $all_first_cat_title .=  '<h2>'.$category->name.'</h2><br>';
                                        
                                            // The Query
                                            $the_query = new WP_Query( 'cat='.$category->term_id);

                                            // The Loop
                                            while ( $the_query->have_posts() ) : $the_query->the_post();
                                                      $all_first_cat_content .= '<div class="produit"><h3><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></h3><br>';
                                                $all_first_cat_content .= '<a href="'.get_permalink($post->ID).'">'.get_the_post_thumbnail($post->ID, array(115,145) ).'</a>';
                                                $all_first_cat_content .=  '<p><span class="excerpt-produit">'.limit_words(get_the_excerpt(), '10').'</span></p></div>';
                                              
                                            endwhile;
                                            
                                            
                                        $i++;

                                } else {

                                    $all_other_cat_content .=  '<h3>'. $category->name .'</h3>';
                                    
                                    $all_other_cat_content .= '<div id="tabs-'. $category->cat_ID .'">';
                                            foreach (get_posts(array('category'=> $category->term_id,'numberposts' => -1)) as $post) {
                                                setup_postdata( $post );
                                                $all_other_cat_content .= '<ol class="other_product"><li><span class="nom-produit"><h3><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></h3></span>';
                                                $all_other_cat_content .= '<span class="image-produit"><a href="'.get_permalink($post->ID).'">'.get_the_post_thumbnail($post->ID, array(115,115) ).'</a></span>';
                                                $all_other_cat_content .= '<span class="excerpt-produit">'.limit_words(get_the_excerpt(), '10').'</span></ol>';
                                               // echo var_dump($post);
                                            }
                                    $all_other_cat_content .= '</div>';
                                    
                                    
                                }

                            }

                            //Return affihcage 
                            echo '<div id="container_produit">'.$all_first_cat_title;  
                            echo $all_first_cat_content;
                            echo '<br><div class="clear"></div><br><br><div id="accordion">' ;
                            echo $all_other_cat_content.'</div></div>' ;
                            
                    } else {
                        while (have_posts()) : the_post();
                            get_template_part("content-post", get_post_format());
                        endwhile;
                    }
                } 
            ?>       

            
            
            
            
            


<?php else : ?>
            <?php get_template_part('content', 'none'); ?>
        <?php endif; ?>

    </div><!-- #content -->
</section><!-- #primary -->

<?php get_sidebar('inside'); ?>
<div class="clear"></div>
<?php get_footer(); ?>